package com.ayuan.blog.controller;

import com.alibaba.fastjson.JSONObject;
import com.ayuan.blog.common.HttpClientUtil;
import com.ayuan.blog.common.WeChatUtil;
import com.ayuan.blog.spider.BuildProcessor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

/**
 * Created by xiaozhu on 15/12/27.
 */
@Controller
@Log4j2
public class TestController {

//    @Autowired
    private BuildProcessor buildProcessor;

    @RequestMapping(value = "/test")
    public ModelAndView test() {
        ModelAndView modelAndView = new ModelAndView("/test");

        // 获取上下文
        SecurityContext securityContext = SecurityContextHolder.getContext();
        // 获取认证对象
        Authentication authentication = securityContext.getAuthentication();
        // 在认证对象中获取主体对象
        Object principal = authentication.getPrincipal();
        String userName = "";
        if (principal instanceof UserDetails) {
            userName = ((UserDetails) principal).getUsername();
        } else {
            userName = principal.toString();
        }

        modelAndView.addObject("msg", userName);
        return  modelAndView;
    }

    @RequestMapping(value = "/spider")
    public ModelAndView spider() {
        buildProcessor.crawl();

        return new ModelAndView("/test");
    }

//    @RequestMapping(value = "/weixin")
//    public ModelAndView weixin(String code) {
//        log.info("code : " + code);
//        String url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=%s&secret=%s&code=%s&grant_type=authorization_code";
//        String appId = "wxe2a6d867fc22c0f9";
//        String secret = "c15dbd7ef74780e85e66b428ab910a02";
//        url = String.format(url, appId, secret, code);
//        log.info("url" + url);
//
//        JSONObject jsonObject = HttpClientUtil.httpPost(url, null, false);
//        String openId = jsonObject.getString("openid");
//        log.info("openId : " + openId);
//
//        String userInfoUrl = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=%s&openid=%s&lang=zh_CN";
//        userInfoUrl = String.format(userInfoUrl, WeChatUtil.getToken(), openId);
//        log.info("userInfoUrl : " + userInfoUrl);
//        JSONObject jsonObject1 = HttpClientUtil.httpPost(userInfoUrl, null, false);
//        log.info("jsonObject1 : " + jsonObject1.toJSONString());
//        String nickName = jsonObject1.getString("nickname");
//        log.info("openId : " + nickName);
//
//        ModelAndView modelAndView = new ModelAndView("/weixin");
//        modelAndView.addObject("openId", nickName);
//
//        return modelAndView;
//    }
}
